1193. 每月交易 I //考察日期函数的格式化输出
# Write your MySQL query statement below
select
    date_format(trans_date,"%Y-%m") as month,
    country,
    count(*) as trans_count,
    sum(if(state="approved",1,0)) as approved_count,
    sum(amount) as trans_total_amount,
    sum(if(state="approved",amount,0)) as approved_total_amount
from
    Transactions 
group by
    country,date_format(trans_date,"%Y-%m")

面试题 03.05. 栈排序    //个人觉得是考察数据结构，这种题在于设计数据结构的开放性，而不要拘泥于栈
class SortedStack 
{
vector<int> stack;
int min_value = INT_MAX;
int sort_falg = 0;

    static bool compare(int x,int y)
    {
        return x>y;
    }
public:
    SortedStack(){}

    void push(int val) 
    {
        if(val<=min_value)
        {
            min_value=val;
            stack.push_back(val);
        }
        else 
        {
            stack.push_back(val);
            sort_falg=1;
        }
    }
    
    void pop() 
    {
        if(sort_falg==1)
        {
            sort_falg=0;
            sort(stack.begin(),stack.end(),compare);
        }

        if(stack.size()==0)
        {
            return;
        }
        else 
        {
            stack.pop_back();
        }
    }
    
    int peek() 
    {
        if(sort_falg==1)
        {
            sort_falg=0;
            sort(stack.begin(),stack.end(),compare);
        }
        return stack.size()==0?-1:stack.back();
    }
    
    bool isEmpty() 
    {
        return stack.size()==0;
    }
};

/**
 * Your SortedStack object will be instantiated and called as such:
 * SortedStack* obj = new SortedStack();
 * obj->push(val);
 * obj->pop();
 * int param_3 = obj->peek();
 * bool param_4 = obj->isEmpty();
 */

 71. 简化路径   //状态机上大分！
 enum Status
{
    CURDIRCTORY,
    PARDIRCTORY,
    MULPOINT,
    SEPCHAR,
    FILENAME
};

class Solution 
{
vector<string> simplypath;
Status status;
public:
    string simplifyPath(string path) 
    {
        path+='/';
        string tmp="/";
        status=SEPCHAR;
        for(int i=0;i<path.size();i++)
        {
            if(path[i]=='/')
            {
                if(status==PARDIRCTORY)
                {
                   if(simplypath.size()!=0)
                   {
                        simplypath.pop_back();
                   }
                }
                else if(status==FILENAME||status==MULPOINT)
                {
                    simplypath.push_back(tmp);
                }
                tmp="/";
                status=SEPCHAR;
            }
            else if(path[i]=='.')
            {
                if(status==CURDIRCTORY)
                {
                    status=PARDIRCTORY;
                }
                else if(status==PARDIRCTORY)
                {
                    status=MULPOINT;
                }
                else if(status==SEPCHAR)
                {
                    status=CURDIRCTORY;
                }
                tmp+='.';
            }
            else 
            {
                tmp+=path[i];
                status=FILENAME;
            }
        }

        string ret;
        for(auto&e : simplypath)
        {
            ret+=e;
        }
        
        if(ret.size()==0)
        {
            ret="/";
        }
        return ret;
    }
};

